<cfparam name="OAP_ID" type="integer" default="0" />
<cfparam name="returnView" type="string" default="users" />
<cfparam name="passback" type="string" default="" />

<cfscript>
	oapGateway = new assets.calgrants.com.gateway.OAPGateway();
	oap = oapGateway.get(OAP_ID = OAP_ID, Program_Year = request.currentProgramYear);
	if (oap.getOAP_ID() eq 0 AND request.authUser.organizationID gt 0) {
		oap.setOrganization_ID(request.authUser.organizationID);
	}
</cfscript>

<cfif IsNumeric(oap.getOAP_ID()) and oap.getOrganization_ID() gt 0>

	<cfscript>
		base = new assets.calgrants.com.Base();
		passbackQueryString = base.passbackToQueryString(passback);
		oapValueGateway = new assets.calgrants.com.gateway.OAPValueGateway();
		oapValues = oapValueGateway.query(Active = 1);
	</cfscript>
	
	<script type="text/javascript" language="javascript">
	<!--
		<cfoutput>
		var oapValues = #SerializeJSON(oapValues, false)#;
		</cfoutput>
	
		validate = function() {
			var isValid = true;
			var errorMessages = [];
			var campusCheckboxes = $(".campusoutreachactivities");
			var parentCheckboxes = $(".outreachtoparents");
			var partnerCheckboxes = $(".outreachtocommunitypartners");
			var campusCount = 0;
			var parentCount = 0;
			var partnerCount = 0;
			var workshopCount = $("#Workshop_Count").val();
			var studentTarget = $("#Student_Target").val();
			var intRegex = /^\d+$/;
			if (!intRegex.test(workshopCount)) {
				isValid = false;
				errorMessages.push("Number of workshops you will conduct is required and must be a number greater than 0");
			} else {
				if (workshopCount < 1) {
					isValid = false;
					errorMessages.push("Number of workshops you will conduct is required and must be a number greater than 0");	
				}
			}
			if (!intRegex.test(studentTarget)) {
				isValid = false;
				errorMessages.push("Anticipated total number of students your workshop\(s\) will assist is required and must be a number greater than or equal to 10");
			} else {
				if (studentTarget < 10) {
					isValid = false;
					errorMessages.push("Anticipated total number of students your workshop\(s\) will assist is required and must be a number greater than or equal to 10");	
				}
			}
			for (var i=0; i<campusCheckboxes.length; i++) {
				if (campusCheckboxes[i].checked) {
					campusCount = campusCount + 1;
				}
			}
			for (var i=0; i<parentCheckboxes.length; i++) {
				if (parentCheckboxes[i].checked) {
					parentCount = parentCount + 1;
				}
			}
			for (var i=0; i<partnerCheckboxes.length; i++) {
				if (partnerCheckboxes[i].checked) {
					partnerCount = partnerCount + 1;
				}
			}
			if (campusCount < 2) {
				isValid = false;
				errorMessages.push("Select at least two items from Campus Outreach Activities");
			}
			if (parentCount < 2) {
				isValid = false;
				errorMessages.push("Select at least two items from Outreach to Parents");
			}
			if (partnerCount < 2) {
				isValid = false;
				errorMessages.push("Select at least two items from Outreach to Community Partners");
			}
			if (errorMessages.length > 0) {
				var errorMessage = "";
				for (var i=0; i<errorMessages.length; i++) {
					errorMessage += errorMessages[i] + "\n";	
				}
				alert(errorMessage);
			}
			return isValid;	
		}
		$(function() {
			$("#oapModal").modal();
			resizeModal("oapModal");	
		})
	//-->
	</script>

	<cfform id="oapForm" name="oapForm" action="#script_name#" method="post" onsubmit="return validate();">
		<cfinput type="hidden" name="action" value="saveOAP" />
		<cfinput type="hidden" name="view" value="#returnView#" />
		<cfinput type="hidden" name="OAP_ID" value="#oap.getOAP_ID()#" />
		<cfinput type="hidden" name="Organization_ID" value="#oap.getOrganization_ID()#" />
		<cfinput type="hidden" name="Created_By_ID" value="#request.authUser.userID#" />
		<cfinput type="hidden" name="Program_Year" value="#request.currentProgramYear#" />
		<cfif IsDefined("passback")>
			<cfloop index="keyValue" list="#passback#" delimiters=";">
				<cfinput type="hidden" name="#ListFirst(keyValue)#" value="#ListLast(keyValue)#" />
			</cfloop>
		</cfif>

		<div id="oapModal" class="modal modal-fullscreen" data-backdrop="static">
			<div class="modal-header">
				<h3>Oureach Action Plan</h3>
			</div>
			<div class="modal-body">
				<div class="container-fluid">
					<div class="row-fluid">
						<div class="span12">
							<p>We are asking you to serve a minimum of 30 students per workshop.</p>
							<p>In order to meet one of the requirements to apply for $300 in site support funding , you will need to return a minimum of 30 student online (or paper) exit surveys. </p>
							<div>
								<label class="required">Number of workshops you will conduct</label>
								<cfinput type="text" id="Workshop_Count" name="Workshop_Count" value="#oap.getWorkshop_Count()#" required="yes" validate="integer" message="Number of workshops you will conduct is required and must be a number" style="width: 100px;" />
							</div>
							<div>
								<label class="required">Anticipated total number of students your workshop(s) will assist</label>
								<cfinput type="text" id="Student_Target" name="Student_Target" value="#oap.getStudent_Target()#" required="yes" validate="integer"  message="Anticipated total number of students your workshop(s) will assist is required and must be a number greater than or equal to 10" style="width: 100px;" />
							</div>
						</div>
					</div>
					<div class="row-fluid">
						<div class="span12">
							<h5>Please indicate which of the following activities will be done to promote your workshop(s).  At least 2 items are required from each of the 3 categories below - Campus, Parent and Community Outreach.</h5>
							<cfset catCount = 1 />
							<cfoutput query="oapValues" group="Category">
								<cfset catGroup = LCase(Replace(Category, " ", "", "all")) />
								<h4>#Category#</h4>
								<div class="well well-framed">
									<cfoutput group="Subcategory">
										<cfif Len(Trim(Subcategory)) gt 0>
											<label>#Subcategory#</label>
										</cfif>
										<cfoutput>
											<label class="checkbox">
												<cfif ListFind(oap.getOAP_Value_IDs(), OAP_Value_ID) gt 0>
													<input type="checkbox" id="OAP_Value_IDs_#OAP_Value_ID#" name="OAP_Value_IDs" class="#catGroup#" value="#OAP_Value_ID#"  checked="checked" />
												<cfelse>
													<input type="checkbox" id="OAP_Value_IDs_#OAP_Value_ID#" name="OAP_Value_IDs" class="#catGroup#" value="#OAP_Value_ID#" />
												</cfif>
												#Description#
											</label>
										</cfoutput>
									</cfoutput>
									<cfif Category eq "Campus Outreach Activities">
										<div class="clearfix">
											<label>Other Campus Outreach Activities</label>
											<textarea name="Campus_Other" class="span9" rows="5"></textarea>
										</div>
									<cfelseif Category eq "Outreach to Parents">
										<div class="clearfix">
											<label>Other Outreach to Parents</label>
											<textarea name="Parent_Other" class="span9" rows="5"></textarea>
										</div>
									<cfelseif Category eq "Outreach to Community Partners">
										<div class="clearfix">
											<label>Other Outreach to Community Partners</label>
											<textarea name="Community_Partner_Other" class="span9" rows="5"></textarea>
										</div>
									</cfif>
									<cfset catCount = catCount + 1 />
								</div>
							</cfoutput>
						</div>
					</div>
				</div>
			</div>
			<div class="modal-footer">
				<cfoutput>
					<a href="?view=#returnView#&#passbackQueryString#" class="btn btn-success">Cancel</a>
					<button type="submit" id="saveButton" name="saveButton" class="btn btn-success">Save</button>
				</cfoutput>
			</div>
		</div>
				
	</cfform>

<cfelse>

	<div class="alert alert-info notFound">Outreach Action Plan not found</div>

</cfif>